package cn.chinasyq.photoquan.user.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import cn.chinasyq.photoquan.R;
import cn.chinasyq.photoquan.user.mode.UserMode;
import cn.master.util.utils.ToastUtil;
import cn.master.volley.models.response.handler.ResponseHandler;


/**
 * 意见反馈
 */
public class FeedbackActivity extends Activity
		implements OnClickListener, ResponseHandler.OnSucceedListener, ResponseHandler.OnNeedLoginListener {

	@SuppressWarnings("unused")
	private static final String TAG = FeedbackActivity.class.getName();

	private ImageButton ibtnBack;
	private Button ibtnCommit;
	private EditText etContent = null;

	// 可输入内容的最大长度
	private int maxLength;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.activity_edit_multiline_text);
		initComponent();
	}

	/**
	 * 初始化组件
	 */
	private void initComponent() {
		ibtnBack = (ImageButton) findViewById(R.id.ibtn_back);
		ibtnCommit = (Button) findViewById(R.id.btn_commit);
		etContent = (EditText) findViewById(R.id.et_content);

		ibtnBack.setOnClickListener(this);
		ibtnCommit.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ibtn_back:
			this.finish();
			break;
		case R.id.btn_commit:
			if (verifyData()) {
				complet();
			}
			break;
		default:
			break;
		}
	}

	private void complet() {
		ResponseHandler handler = new ResponseHandler(null);
		handler.setOnSucceedListener(this);
		handler.setOnNeedLoginListener(this);
		UserMode.sendFeedback(getApplicationContext(), etContent.getText().toString(), handler);
	}
	/**
	 * 验证数据
	 * 
	 * @return
	 */
	private boolean verifyData() {
		if (TextUtils.isEmpty(etContent.getText().toString().trim())) {
			ToastUtil.showTips(getApplicationContext(),R.mipmap.tips_warning,
					getString(R.string.please_input_content));
			return false;
		}
		return true;
	}

	/**
	 * 评论成功
	 */
	@Override
	public void onSucceed(String tag, boolean isCache, Object data) {
		ToastUtil.show(this,"提交成功");
		finish();
	}

	@Override
	public void finish() {
		if (getCurrentFocus() != null) {
			((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
					.hideSoftInputFromWindow(
							getCurrentFocus().getWindowToken(),
							InputMethodManager.HIDE_NOT_ALWAYS);
		}
		super.finish();
	}

	@Override
	public void onNeedLogin(String tag) {
		startActivity(new Intent(FeedbackActivity.this, LoginActivity.class));
	}
}
